package b1w1nn1ng.crossf1t.business;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.xml.crypto.Data;

import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;

import b1w1nn1ng.crossf1t.db.Database;

/**
 * Exercise.java
 * 
 * Contains the fields needed to record an individual exercise within a workout.
 * Any fields which are not used should be initialized to -1.
 * 
 * @author Chris
 */
public class Exercise {
	private int reps;
	private String type;//the name of the exercise
	private int weights;
	private int time;//in seconds
	private Calendar date;

	public Exercise(int userID, String type, int reps,int weights) throws SQLException, ClassNotFoundException {
		this.reps = reps;
		this.type = type;
		this.weights = weights;
		Workout.incrementWorkoutCount(userID);
	}
	
	public Exercise() {
		type = "None";
		weights = -1;
		reps = -1;
		time = -1;
		date = Calendar.getInstance();
	}
	//use this constructor for testing
	public Exercise(String type, int weight, int reps, int time, Calendar date) {
		this.type = type;
		this.weights = weight;
		this.reps = reps;
		this.time = time;
		this.date = date;
	}

	public Exercise(String type, int weight, int reps, int time, String date) throws ParseException, java.text.ParseException {
		this.type = type;
		this.weights = weight;
		this.reps = reps;
		this.time = time;
		this.date = CalendarDisplay.stringToCalendar(date);
	}
	
	public String toHTML() {
		StringBuilder ret = new StringBuilder();
		ret.append("Exercise type: "+type+"<br />");
		if(weights != -1)
			ret.append("Weight: "+weights+"<br />");
		if(reps != -1)
			ret.append("Reps: "+reps+"<br />");
		if(time != -1)
			ret.append("Time: "+time+"<br />");
			
		return ret.toString();
	}

	/**
	 * This method determines if ex is a new Person Record for type PRType.
	 * Does NOT update the database to make the exercise the new PR.
	 * 
	 * @param ex - the exercise being compared
	 * @param exerciseType - 0: for time
	 * 						1: for reps
	 * 						2: for weight
	 * @return true if the exercise is a new PR
	 * @throws Exception 
	 */
	public static boolean isNewPR(int userID, Exercise ex, String PRType) throws Exception{
		if(!(PRType.equals("Time")||PRType.equals("Reps")||PRType.equals("Weight")))
			throw new Exception("excerseType does not exist");
		
		//determine score
		//determin prev score
//		Database.getPR()
		
		
		/*
		 * getPR(String exerciseType, PRType) should return an int for the personal record
		 * of exerciseType. PRType is described in the javadoc above this method.
		 * 
		 */
		int PR = Exercise.getPRExercise(userID,ex.getType(),PRType);
		
		if(PRType.equals("Time")) 
		{
				if(PR>ex.getTime())
					return true;
		}
		else if(PRType.equals("Reps"))
		{
				if(PR<ex.getReps())
					return true;
		}
		else if(PRType.equals("Weight"))
		{	
				if(PR<ex.getWeights())
					return true;
		}
		return false;
	}
	
	/**
	 * getPRExercise(String exerciseType, String PRType) should return an int for
	 * the personal record of exerciseType. (PR for a specific exercise)
	 * 
	 * Exercise Type "Time" (in seconds), "Reps", "Weight" (in pounds) are the only categories.
	 * 
	 * @param type
	 * @param pRType
	 * @return
	 */
	public static int getPRExercise(int id, String pRType, String exerciseType) {
		return Database.getPRExercise(id, pRType, exerciseType);
	}
	
	public int getWeights() {
		return weights;
	}


	public void setWeights(int weights) {
		this.weights = weights;
	}


	public int getTime() {
		return time;
	}


	public void setTime(int time) {
		this.time = time;
	}


	public int getReps() {
		return reps;
	}
	public void setReps(int reps) {
		this.reps = reps;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}

	public Calendar getDate() {
		return date;
	}

	public String getFormattedDate() {
		SimpleDateFormat dfm = new SimpleDateFormat("MM/dd/yyyy");
		return dfm.format(date);
	}
	public void setDate(Calendar date) {
		this.date = date;
	}

	/*
	 * still relevant????
	 * 
	public void setFormattedDate(String setDate) throws ParseException, java.text.ParseException {
		SimpleDateFormat dfm = new SimpleDateFormat("MM/dd/yyyy");
		Date d = dfm.parse(setDate);
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		date = c;
	}
	*/
	
}
